/**
 * 
 */
package com.qiyou.dao.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.qiyou.dao.IBlogCommentDao;
import com.qiyou.entity.BlogComment;
import com.qiyou.util.DBUtil;
import com.qiyou.util.TransformUtil;

/**
 * 博客评论信息实现
 * 
 * @author 赛艳志
 * 
 */
public class BlogCommentDaoImpl implements IBlogCommentDao {
    private Connection conn = null;

    public BlogCommentDaoImpl(Connection conn) {
        this.conn = conn;
    }

    @Override
    public boolean doCreate(BlogComment blogComment) throws SQLException {
        String sql = "insert into tb_blog_comment(userID,blogID,type,motherID,bcContent,bcScore,bcDate) values(?,?,?,?,?,?,?)";
        Object[] params = TransformUtil.beanTOArray(blogComment, true);
        if (blogComment.getMotherID()<=0) {
            params[3] = null;
        }
        params[6] = new Timestamp(blogComment.getDate().getTime());
        return DBUtil.execute(conn, sql, params);
    }
    
    @Override
    public boolean doUpdate(BlogComment blogComment) throws SQLException {
        String sql = "update tb_blog_comment set bcID=?,userID=?,blogID=?,type=?,motherID=?,bcContent=?,bcScore=?,bcDate=? where bcID=?";
        Object[] params = TransformUtil.beanTOArray(blogComment, false);
        if (blogComment.getMotherID()<=0) {
            params[4] = null;
        }
        return DBUtil.execute(conn, sql, params,params[0]);
    }

    @Override
    public boolean doDelete(long blogCommentID) throws SQLException {
        String sql = "delete from tb_blog_comment where bcID=?";
        return DBUtil.execute(conn, sql, blogCommentID);
    }

    @Override
    public BlogComment getBlogComment(long blogCommentID) throws SQLException {
        BlogComment blogComment = null;
        String sql = "select * from tb_blog_comment where bcID=?";
        List<Object []> all = DBUtil.query(conn, sql, blogCommentID);
        if (null!=all&&all.size()>0) {
            Object[] objects = all.get(0);
            blogComment = (BlogComment) TransformUtil.arrayToBean(objects, BlogComment.class);
        }
        return blogComment;
    }

    @Override
    public List<BlogComment> getBlogComments(String userID) throws SQLException {
        List<BlogComment> bclist = null;
        String sql = "select * from tb_blog_comment where userID=?";
        List<Object []> all = DBUtil.query(conn, sql, userID);
        if (null!=all&&all.size()>0) {
            BlogComment blogComment = null;
            bclist = new ArrayList<BlogComment>(all.size());
            for(Object[] objects:all){
                blogComment = (BlogComment) TransformUtil.arrayToBean(objects, BlogComment.class);
                bclist.add(blogComment);
            }
        }
        return bclist;
    }

    @Override
    public List<BlogComment> getBlogComments(long blogID) throws SQLException {
        List<BlogComment> bclist = null;
        String sql = "select * from tb_blog_comment where blogID=?";
        List<Object []> all = DBUtil.query(conn, sql, blogID);
        if (null!=all&&all.size()>0) {
            BlogComment blogComment = null;
            bclist = new ArrayList<BlogComment>(all.size());
            for(Object[] objects:all){
                blogComment = (BlogComment) TransformUtil.arrayToBean(objects, BlogComment.class);
                bclist.add(blogComment);
            }
        }
        return bclist;
    }

    @Override
    public List<BlogComment> getBlogComments(Object... params)
            throws SQLException {
        List<BlogComment> bclist = null;
        StringBuffer sql = new StringBuffer("select * from tb_blog_comment ");
        if (null != params && params.length > 0) {
            for (Object param : params) {
                sql.append(param);
            }
        }
        List<Object []> all = DBUtil.query(conn, sql.toString());
        if (null!=all&&all.size()>0) {
            BlogComment blogComment = null;
            bclist = new ArrayList<BlogComment>(all.size());
            for(Object[] objects:all){
                blogComment = (BlogComment) TransformUtil.arrayToBean(objects, BlogComment.class);
                bclist.add(blogComment);
            }
        }
        return bclist;
    }

}
